#!/usr/bin/perl
# ex7-4 : Mutate Protein .
# use a random number generator to randomly select bases to mutate .

#TODO:
#2012-03-26 : add '^' to show the difference between two strings.

use 5.010;
use strict;
use warnings;

#declare the variables
#The Protein is chosen to make it easy to see mutations.
my $protein = 'PPPPPPPPPPPPPPPPPPPPPPPPP';
my $i;
my $mutant;

# Seed the random number generator.
# time|$$ combines the current time and the current process ID
srand(time|$$);

# show the original.
say "original Protein : ".$protein;

# let's mutate it ! (the first time !)
$mutant = mutate($protein);

for ($i = 0 ;$i<10 ; $i++){
	$mutant = mutate($mutant);
#	say "here is the mutant Protein\t : ".$mutant;
}

################################
# subroteins for protein mutant
################################
sub mutate{
	my($protein) = @_;
	my(@amino_acid)=('G','A','V','I','L','P',	# R-hydrophobic:  glycine,alanine,valine,isoleucine,leucine,proline
			 'W','F','Y',			# R-aromatic-compound : tryptophan,	
			 'S','T','C','M','N','G',	# R-polarity-non-electirc-charge : Serine,Threonine,Cysteine,Methionine,Asparagine,Glutamic acid
			 'D','E',			# R-negative-charge : Aspartic acid,Glutamic acid
			 'K','R','H'			# R-positive-charge : Lysine,Arginine,Histidine
			);
	my($position) = random_position($protein);
	my($new_base) = random_amino_acid(@amino_acid);
	substr($protein,$position,1,$new_base);
	
	say "Mutant Protein \t : $protein ";
	return $protein;
}

################################
# random_position
#
# A subroutine to randomly select a position in a string.
#
# Warning : make sure you have call srand to seed the random number generator
#	    before call this function.
################################
sub random_position{
	my($string) = @_;
	return int rand length $string;
}
################################
#random_amino_acid
#
#A subroutine to select at random 20 of the amino acids.
################################
sub random_amino_acid{
	my(@amino_acids) = @_;
	return random_element(@amino_acids);
}

################################
# random_element
#
# A subroutine to randomly select an element from an array.
################################
sub random_element{
	my(@array) = @_;
	return $array[rand @array];
}
